const polyfill = require('./polyfill')
const parseConfig = require('./parseConfig')
const exportExcel = require('./excelExport')
const globalConfig = require('./config')
const startSpider = require('./startSpider')
const fs = require('fs')

polyfill()

module.exports = parseConfig('../config.txt')
.then((config) => {
  globalConfig.config = config
  console.info('开始抓取安卓信息')  
  return startSpider('android')
})
.then(list => {
  fs.writeFileSync('android.json', JSON.stringify(list, null, 2))
  return exportExcel({
    title: globalConfig.config.androidExcelName,
    index: ['appname', 'count', 'baidu', 'tencent', 'qh360'],
    headers: {
      appname: '产品名称',
      count: '全平台总下载量',
      baidu: '百度评分',
      tencent: '应用宝评分',
      qh360: '360评分'
    },
    list
  })
})
.then(() => {
  console.info('安卓数据抓取完成')
})
.catch((err) => {
  console.error(err)
})